package net.icez.upicme;

/**
 * com.sugree.jibjib.OAuthService
 * source: http://code.google.com/p/jibjib/source/browse/trunk/android/src/com/sugree/jibjib/OAuthService.java
 **/
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import net.icez.twitter.PrivateData;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

public class OAuthService {
        public static final String TAG = "OAuthService";

        private Context mContext;
        private SharedPreferences mSP;
        private OAuthConsumer mHttpOauthConsumer;
        private OAuthProvider mHttpOauthProvider;
        private String mRequestTokenUrl;
        private String mAccessTokenUrl;
        private String mAuthorizeUrl;

        public OAuthService(Context context) {
                mContext = context;
                PreferenceManager.setDefaultValues(mContext, R.xml.preferences, false);
                mSP = PreferenceManager.getDefaultSharedPreferences(mContext);

                mRequestTokenUrl = mSP.getString("oauth_request_token", "https://api.twitter.com/oauth/request_token");
                mAccessTokenUrl = mSP.getString("oauth_access_token", "https://api.twitter.com/oauth/access_token");
                mAuthorizeUrl = mSP.getString("oauth_authorize", "https://api.twitter.com/oauth/authorize");
        }

        public String doOAuth() {
                /*
                Log.d(TAG, "consumer key "+PrivateData.OAUTH_CONSUMER_KEY);
                Log.d(TAG, "consumer secret "+PrivateData.OAUTH_CONSUMER_SECRET);
                Log.d(TAG, "request token url "+mRequestTokenUrl);
                Log.d(TAG, "access token url "+mAccessTokenUrl);
                Log.d(TAG, "authorize url "+mAuthorizeUrl);
                */

                try {
                        mHttpOauthConsumer = new CommonsHttpOAuthConsumer(PrivateData.OAUTH_CONSUMER_KEY, PrivateData.OAUTH_CONSUMER_SECRET);
                        mHttpOauthProvider = new CommonsHttpOAuthProvider(mRequestTokenUrl, mAccessTokenUrl, mAuthorizeUrl);
                        String authUrl = mHttpOauthProvider.retrieveRequestToken(mHttpOauthConsumer, PrivateData.OAUTH_CALLBACK_URL);
                        /*
                        Log.d(TAG, "auth url "+authUrl);
                        */
                        mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
                } catch (Exception e) {
                        Log.d(TAG, "oauth exception "+e.getMessage());
                        return e.getMessage();
                }
                return null;
        }

        public String callbackOAuth(Uri uri) {

                String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
                try {
                        mHttpOauthProvider.retrieveAccessToken(mHttpOauthConsumer, verifier);
                        String accessToken = mHttpOauthConsumer.getToken();
                        String tokenSecret = mHttpOauthConsumer.getTokenSecret();
                        String username = mHttpOauthProvider
                                        .getResponseParameters()
                                        .getFirst("screen_name");
                        /*
                        Log.d(TAG, "access token "+accessToken);
                        Log.d(TAG, "token secret "+tokenSecret);
                        */

                        SharedPreferences.Editor esp = mSP.edit();
                        if (username != null) {
                                esp.putString("twitter_username", username);
                        }
                        esp.putString("oauth_access_token", accessToken);
                        esp.putString("oauth_token_secret", tokenSecret);
                        esp.putBoolean("use_oauth", true);
                        Log.d(TAG, "access token: " + accessToken);
                        Log.d(TAG, "token secret: " + tokenSecret);
                        esp.commit();
                } catch (Exception e) {
                        Log.d(TAG, "oauth exception "+e.getMessage());
                        return e.getMessage();
                }
                return null;
        }
}
